/** 
* BaseServiceImpl.java Created on Apr 27, 2009
* Copyright 2009@JSHX. 
* All right reserved. 
*/
package com.lynn.mmcc.common.dao;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.lynn.mmcc.common.exception.AppBaseException;
import com.lynn.mmcc.tools.Configuration;
import com.lynn.mmcc.tools.DateTimeUtil;


/**
 * 业务service基类
 * @Time 11:10:41 AM
 * @author mengxiankong
 */
public class BaseServiceImpl implements BaseService{
	/**
	 * 数据库访问接口
	 */
	protected IbatisBaseDao dao;
	
	/**
	 * 数据库访问模板
	 * @return
	 */
	protected SqlMapClientTemplate getTemplate(){
		return getDao().getSqlMapClientTemplate();
	}
	/**
	 * 数据库访问客户端
	 * @return
	 */
	protected SqlMapClient getSqlMap() {
		return getDao().getSqlMapClient();
	}
	/**
	 * 对象为空 抛出异常
	 * @param obj
	 * @throws AppBaseException
	 */
	protected void checkNullObject(Object obj) throws AppBaseException{
		if(obj==null) 
			throw new AppBaseException("error.invalid.isnull"); 
	}
	/**
	 * 对象不空 抛出已存在异常
	 * @param obj
	 * @param msg
	 * @throws AppBaseException
	 */
	protected void checkExistObject(Object obj, String msg) throws AppBaseException{
		if(obj != null) 
			throw new AppBaseException("error.invalid.exist",msg); 
	}

	/**
	 * @return Returns the dao.
	 */
	public IbatisBaseDao getDao() {
		return dao;
	}

	/**
	 * @param dao The dao to set.
	 */
	public void setDao(IbatisBaseDao dao) {
		this.dao = dao;
	}
	  

	    
	    public Integer getMaxid(){
	    	Integer maxid= (Integer)this.getDao().getSqlMapClientTemplate().queryForObject("maxfileupload");
			return maxid;
	    }
	    
	    private  String uploadFileAct(String fileName, File file,String pathnameinproperties) {		
			String destFileName = null;
			//long now = new Date().getTime();
			String time=DateTimeUtil.getDateTime(new Date(), "yyyy-MM-dd");
			String[] timeutil=time.split("-");
	        destFileName = timeutil[0]+"\\"+timeutil[1]+"\\"+timeutil[2]+"\\" + fileName;
			Configuration cfg=new Configuration(null,"systemupload.properties");
	        String uploadPath=cfg.getValue(pathnameinproperties);

			String revPath = uploadPath+ "/" + destFileName;
			File destFile = new File(revPath);

			try {
				FileUtils.copyFile(file, destFile);
			} catch (IOException e) {
				return null;
			}
			return revPath;
		}
	   
	    
	    public  String getISOFromGBK(String source)
		{
			String result = null;
			try {
				result = new String(source.getBytes("GBK"),"iso-8859-1");
			} catch (UnsupportedEncodingException e) {
				return null;
			}
			return result;
		}
	    public InputStream getDownLoadFileStream(String fileName) {
			InputStream downFileStream = null;
			FileInputStream fis = null;
			ByteArrayOutputStream output = null;
			File file = new File(fileName);
			try {
				fis = new FileInputStream(file);
				output = new ByteArrayOutputStream();
				// 缓冲
				byte[] buffer = new byte[1024];
				int iLength = 0;
				// 将文件写入字节输出流
				while ((iLength = fis.read(buffer)) != -1) {
					output.write(buffer, 0, iLength);
				}
				// 将得到的字节输出流转换成字节数组——>字节输入流
				downFileStream = new ByteArrayInputStream(output.toByteArray());

			} catch (Exception e) {
				// logger.error(e, e);
				return null;

			} finally {
				try {
					if (fis != null) {
						fis.close();
						fis = null;
					}
					if (output != null) {
						output.close();
						output = null;
					}
				} catch (Exception e) {

				}
			}
			return downFileStream;
		}

}
